#!/bin/bash
#chmod +x nagcent
set -e

nagios_version="3.2.3"
nagios_plugins_version="1.4.15"
nrpe_version="2.12"
ndoutils_version="1.4b9"
centreon_version="2.1.10"

#
# Installation des dépendances
#

aptitude install -y mailutils build-essential sudo apache2 apache2-mpm-prefork php5 php5-mysql php-pear php5-ldap php5-snmp php5-gd mysql-server libmysqlclient15-dev rrdtool librrds-perl libconfig-inifiles-perl libcrypt-des-perl libdigest-hmac-perl libdigest-sha1-perl libgd-gd2-perl snmp snmpd libnet-snmp-perl libsnmp-perl libgd2-xpm libgd2-xpm-dev libpng12-dev gettext libssl-dev

#
# Mise à jour de PHP-PEAR
#

pear channel-update pear.php.net
pear upgrade pear
pear upgrade-all
pear install -f SOAP Validate XML_RPC
pear install -f DB DB_DataObject DB_DataObject_FormBuilder
pear install -f Archive_Tar Auth_SASL Console_Getopt Date
pear install -f HTML_Common HTML_QuickForm 
pear install -f HTML_QuickForm_advmultiselect HTML_Table
pear install -f HTTP_Request Image_GraphViz Log MDB2
pear install -f Net_Ping Net_SMTP Net_Socket Net_Traceroute Net_URL
pear install -f Structures_Graph


#
# Création de l'utilisateur et du groupe nagios 
#

if [ $(grep -c "^nagios:" /etc/passwd) -eq 0 ]
then
	/usr/sbin/useradd -m nagios
	passwd nagios
fi

#
# Création du groupe nagcmd
#

if [ $(grep -c "^nagcmd:" /etc/group) -eq 0 ]
then
	groupadd nagcmd
fi

#
# On ajoute nagios et www-data aux groupes nagios et nagcmd
#

/usr/sbin/usermod -G nagios,nagcmd nagios
/usr/sbin/usermod -G nagios,nagcmd www-data

#
#Install Nagios
#

cd /usr/local/src/

archive_nagios="nagios-$nagios_version.tar.gz"
if [ ! -f $archive_nagios ]
then
	wget http://download.tech-max.fr/nagios/common/setup_files/$archive_nagios
fi

tar xvzf $archive_nagios
cd nagios-$nagios_version/
./configure --prefix=/usr/local/nagios --with-command-group=nagcmd --enable-nanosleep --enable-event-broker
make all install install-init install-commandmode install-config

#
#Install Nagios Web Interface
#

cd /usr/local/src/nagios-3.2.3/
make install-webconf
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
/etc/init.d/apache2 reload

#
#Install Nagios plugins
#

cd /usr/local/src/
archive_nagios_plugins="nagios-plugins-$nagios_plugins_version.tar.gz"
if [ ! -f $archive_nagios_plugins ]
then
	wget http://download.tech-max.fr/nagios/common/setup_files/$archive_nagios_plugins
fi
	
tar xvzf $archive_nagios_plugins 
cd nagios-plugins-$nagios_plugins_version/
./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl=/usr/bin/openssl
make
make install

#
# Install NRPE
#

cd /usr/local/src/
archive_nrpe="nrpe-$nrpe_version.tar.gz"
if [ ! -f $archive_nrpe ]
then
	wget http://download.tech-max.fr/nagios/common/setup_files/$archive_nrpe
fi

tar xvzf $archive_nrpe
chmod 755 nrpe-$nrpe_version
cd nrpe-$nrpe_version/
./configure --enable-ssl --with-ssl 
make all
make install
cp init-script.debian /etc/init.d/nrpe
chmod a+rx /etc/init.d/nrpe
cp sample-config/nrpe.cfg /usr/local/nagios/etc/


#
#Install NDO
#

cd /usr/local/src/
archive_ndoutils="ndoutils-$ndoutils_version.tar.gz"
if [ ! -f $archive_ndoutils ]
then
	wget http://download.tech-max.fr/nagios/common/setup_files/$archive_ndoutils
fi
tar xvzf $archive_ndoutils 

#
# Installation du patch
#

cd ndoutils-$ndoutils_version
wget http://download.tech-max.fr/nagios/common/setup_files/ndoutils${ndoutils_version}_light.patch
patch -p1 -N < ndoutils${ndoutils_version}_light.patch
./configure --prefix=/usr/local/nagios/ --enable-mysql --disable-pgsql \
   --with-ndo2db-user=nagios --with-ndo2db-group=nagios
make
cp ./src/ndomod-3x.o /usr/local/nagios/bin/ndomod.o
cp ./src/ndo2db-3x /usr/local/nagios/bin/ndo2db
cp ./config/ndo2db.cfg-sample /usr/local/nagios/etc/ndo2db.cfg
cp ./config/ndomod.cfg-sample /usr/local/nagios/etc/ndomod.cfg
chmod 770 /usr/local/nagios/bin/ndo*
chown nagios:nagios /usr/local/nagios/bin/ndo*
cp ./daemon-init /etc/init.d/ndo2db
chmod +x /etc/init.d/ndo2db

#
# Install Centreon
#

cd /usr/local/src/
wget http://download.tech-max.fr/nagios/common/setup_files/centreon-$centreon_version.tar.gz
wget http://download.tech-max.fr/nagios/Debian/debian.si
tar xvzf centreon-$centreon_version.tar.gz
cd centreon-$centreon_version/
export PATH="$PATH:/usr/local/nagios/bin/"
./install.sh -f /usr/local/src/debian.si
/etc/init.d/apache2 reload

#
# Langue Centreon
#

# En toute rigueur il faudrait faire locale=$LANG mais on n'est pas sûr de trouver toutes
# les langues sur http://download.tech-max.fr/

lang="fr_FR"
locale="$lang.UTF-8"
centreon_lang_version="2.1-$lang-1"

mkdir -p /usr/local/centreon/www/locale/$locale/LC_MESSAGES/
cd /usr/local/src/
archive_nagios_lang="centreon-$centreon_lang_version.tgz"
if [ ! -f $archive_nagios_lang ]
then
	wget http://download.tech-max.fr/nagios/common/setup_files/$archive_nagios_lang
fi
tar xvzf $archive_nagios_lang 
#cd centreon-$centreon_lang_version/LC_MESSAGES
cd centreon-$centreon_lang_version.1/LC_MESSAGES
cp messages.mo /usr/local/centreon/www/locale/$locale/LC_MESSAGES/messages.mo
read -p "Se connecter à http://Ip/centreon, suivre les étapes de configuration de l'interface Centreon et à la fin appuyer sur une touche pour finaliser..."

#
# Ajout des services ndo2db et nagios
#

update-rc.d ndo2db defaults
update-rc.d nagios defaults
update-rc.d nrpe defaults
/etc/init.d/ndo2db start
/etc/init.d/nagios start
/etc/init.d/nrpe start

#
# On vérifie que tout est OK
#

if [ $(grep -c "ndomod" /usr/local/nagios/var/nagios.log) -ne 0 ]
then
	grep ndomod /usr/local/nagios/var/nagios.log
fi
